package com.qd.panda.card.third;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.common.panda.domain.entity.card.third.sync.ThirdPartySyncDTO;
import com.qd.panda.service.card.third.sync.ThirdPartySyncHolderService;
import com.qd.panda.service.card.third.sync.ThirdPartySyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;

/**
 * 优惠券/第三方/WEB_第三方劵同步
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/thirdPartySync/")
@Slf4j
public class ThirdPartySyncController {
    private final ThirdPartySyncService thirdPartySyncService;
    private final ThirdPartySyncHolderService thirdPartySyncHolderService;

    /**
     * 添加
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PreAuthorize("@ps.hasPermission('system:thirdPartySync:save')")
    @PostMapping(value = "/save")
    public ThirdPartySyncDTO save(@Validated({DefaultGroup.class}) @RequestBody ThirdPartySyncDTO dto) {
        dto.setOperator(SecurityUtils.getLoginUser().getUserId());
        dto.setOrgId(SecurityUtils.getLoginUser().getCurrentOrgId());
        dto.setSyncTime(LocalDateTime.now());
        return thirdPartySyncHolderService.save(dto);
    }


    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:thirdPartySync:delete')")
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        thirdPartySyncHolderService.removeBatchIds(delDTO.getIds());
        return ApiResult.success("删除成功");
    }

    /**
     * 获取
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ThirdPartySyncDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return thirdPartySyncService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ThirdPartySyncDTO> page(ThirdPartySyncDTO dto) {
        return thirdPartySyncService.page(PageUtil.buildPage(dto), dto);
    }
}